package com.zckj.jcServer;


import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

public class Client {
	
	private String  data;
	
	
	 private final String host;
	 private final int port;

	    public Client(String host, int port,String data) {
	        this.host = host;
	        this.port = port;
	        this.data = data;
	    }

	    public void start() throws Exception {
	        EventLoopGroup group = new NioEventLoopGroup();
	        try{
	            Bootstrap b = new Bootstrap();
	            b.group(group)
	                .channel(NioSocketChannel.class)
	                .remoteAddress(new InetSocketAddress(host,port))
	                .handler(new ChannelInitializer<SocketChannel>() {
	                @Override
	                public void initChannel(SocketChannel ch) 
	                            throws Exception {
	                	ch.pipeline().addLast(new LineBasedFrameDecoder(1024));//粘包和拆包问题处理
	                	ch.pipeline().addLast(new StringDecoder());//把接收的字节转为字符串
	                    ch.pipeline().addLast(new ClientHandle(data));
	                }
	            });
	            ChannelFuture f = b.connect().sync();

	            f.channel().closeFuture().sync();
	        }finally {
	            group.shutdownGracefully().sync();
	        }
	    }

	    public static void main(String[] args) throws Exception {
//	        if(args.length != 2){
//	            System.err.println(
//	                    "Usage :" + Client.class.getSimpleName()+
//	                    "<host><port>"
//	                    );
//	            return;
//	        }
	    	
//	        String host = args[0];
//	        int port = Integer.parseInt(args[1]);
	       String  host = "127.0.0.1";
	       int port = 1207;
	       String data = "7df7df7df7df38000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
	        new Client(host,port,hexStringToString(data)).start();
	    }


	/**
	 * 字符串转化成为16进制字符串
	 * @param s
	 * @return
	 */
	public static String strTo16(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	/**
	 * 16进制字符串转换为字符串
	 *
	 * @param s
	 * @return
	 */
	public static String hexStringToString(String s) {
		if (s == null || s.equals("")) {
			return null;
		}
		s = s.replace(" ", "");
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(
						s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "utf-8");
			new String();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}
}
